import 'package:flutter/material.dart';
import 'package:wechat_hd/const_app.dart';
import 'convercation_page.dart';

class NavigationIconView {
  final String _title;
  final IconData _icon;
  final IconData _acvIcon;
  final BottomNavigationBarItem item;

  NavigationIconView({Key key, String title, IconData icon, IconData acvIcon})
      : _title = title,
        _icon = icon,
        _acvIcon = acvIcon,
        item = BottomNavigationBarItem(
            icon: Icon(icon, color: Color(ConstApp.TabNormalColor)),
            activeIcon: Icon(acvIcon, color: Color(ConstApp.TabActiveColor)),
            title: Text(
              title,
              style: TextStyle(
                  color: Color(ConstApp.TabNormalColor), fontSize: 14.0),
            ),
            backgroundColor: Colors.white);
}

class HomeScreen extends StatefulWidget {
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  List<NavigationIconView> _navigationViews;
  int _currentIndex = 0;
  PageController _pageController;
  List<Widget> _pages;

  @override
  void initState() {
    super.initState();

    _pageController = PageController(initialPage: _currentIndex);

    _pages = [
      Convercation(),
      Container(
        color: Colors.blue,
      ),
      Container(
        color: Colors.deepOrange,
      ),
      Container(
        color: Colors.yellow,
      )
    ];

    _navigationViews = [
      NavigationIconView(
          title: '微信',
          icon: IconData(0xe61b, fontFamily: AppFont.IconFontFamily),
          acvIcon: IconData(0xe625, fontFamily: AppFont.IconFontFamily)),
      NavigationIconView(
          title: '通讯录',
          icon: IconData(0xe6c2, fontFamily: AppFont.IconFontFamily),
          acvIcon: IconData(0xe663, fontFamily: AppFont.IconFontFamily)),
      NavigationIconView(
          title: '发现',
          icon: IconData(0xe663, fontFamily: AppFont.IconFontFamily),
          acvIcon: Icons.access_alarm),
      NavigationIconView(
          title: '我',
          icon: IconData(0xe63f, fontFamily: AppFont.IconFontFamily),
          acvIcon: Icons.access_alarm)
    ];
  }

  _buildPopItem(int icon, String title, String value) {
    var item = PopupMenuItem(
      child: Row(
        children: <Widget>[
          Icon(IconData(icon, fontFamily: AppFont.IconFontFamily)),
          Container(
            width: 8.0,
          ),
          Text(
            title,
            style: TextStyle(
              fontSize: 14,
            ),
          )
        ],
      ),
      value: value,
    );
    return item;
  }

  @override
  Widget build(BuildContext context) {
    var container = Container(
      child: PageView.builder(
        itemBuilder: (BuildContext context, int index) {
          return _pages[index];
        },
        itemCount: _pages.length,
        controller: _pageController,
        onPageChanged: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );

    final BottomNavigationBar bottomNavigationBar = BottomNavigationBar(
      items: _navigationViews.map((NavigationIconView view) {
        return view.item;
      }).toList(),
      currentIndex: _currentIndex,
      type: BottomNavigationBarType.fixed,
      onTap: (int index) {
        setState(() {
          _currentIndex = index;
          _pageController.animateToPage(_currentIndex,
              duration: Duration(microseconds: 100), curve: Curves.bounceIn);
        });
      },
    );
    var list = [
      IconButton(
        icon: Icon(
          IconData(
            0xe624,
            fontFamily: AppFont.IconFontFamily,
          ),
          size: 22,
        ),
        onPressed: () {
          print('点击了搜索按钮');
        },
      ),
      Container(
        width: 16,
      ),
      PopupMenuButton(
        icon: Icon(
          IconData(0xe61e, fontFamily: AppFont.IconFontFamily),
          size: 22,
        ),
        itemBuilder: (BuildContext context) {
          return <PopupMenuItem<String>>[
            _buildPopItem(0xe661, '发起群聊', 'grop_type'),
            _buildPopItem(0xe7dc, '添加好友', 'ad_type'),
            _buildPopItem(0xe661, '扫一扫', 'scan_type'),
            _buildPopItem(0xe6c4, '收付款', 'pay_type'),
            _buildPopItem(0xe77c, '反馈和帮助', 'help_type'),
          ];
        },
        onSelected: (type) {
          print(type);
        },
      ),
      Container(
        width: 16,
      )
    ];
    var scaffold = Scaffold(
      appBar: AppBar(title: Text('微信'), elevation: 0.0, actions: list),
      body: container,
      bottomNavigationBar: bottomNavigationBar,
    );
    return scaffold;
  }
}
